<template>
	<view>
		<view class="navigation">
			<view class="navigation_back" @tap="backref()">
				<uni-icons type="back" size="24" color="#fff"></uni-icons>
			</view>
			<view class="navigation_title">
				编辑地址
			</view>
		</view>
		<view class="address_con">
			<view class="address_item address_name">
				<view class="address_icon">
					<!-- <image src="../../../static/image/memberCenter/address_name.svg" mode=""></image> -->
					姓名
				</view>
				<view class="address_input">
					<input v-model="detail.userName" type="text" placeholder="请输入您的姓名" placeholder-class="placeholder">
				</view>
			</view>
			<view class="address_item address_phone">
				<view class="address_icon">
					<!-- <image src="../../../static/image/memberCenter/address_phone.svg" mode=""></image> -->
					电话
				</view>
				<view class="address_input">
					<input v-model="detail.userPhone" type="text" placeholder="请输入电话号码">
				</view>
			</view>
			<view class="address_item address_building">
				<view class="address_icon">
					<!-- <image src="../../../static/image/memberCenter/address_build.svg" mode=""></image> -->
					寝室
				</view>
				<view class="address_input">
					<radio-group  @change="setOptions">
						<label>
							<radio value="男寝" :checked="add.sex == '男寝'" /><text>男寝</text>
						</label>
						<label>
							<radio value="女寝" :checked="add.sex == '女寝'" /><text>女寝</text>
						</label>
					</radio-group>
					<!-- <input v-model="address.userAddress"  type="text" placeholder="请填写详细地址"> -->
				</view>
			</view>
			<view class="address_item address_plate">
				<view class="address_icon">
					<!-- <image src="../../../static/image/memberCenter/address_plate.svg" mode=""></image> -->
					楼栋
				</view>
				<view class="address_input">
					<input type="text" v-model="add.build" placeholder="请输入楼栋号">
				</view>
			</view>
			<view class="address_item address_plate">
				<view class="address_icon">
					<!-- <image src="../../../static/image/memberCenter/address_plate.svg" mode=""></image> -->
					详细地址
				</view>
				<view class="address_input">
					<input type="text" v-model="add.detail" placeholder="请输入寝室号">
				</view>
			</view>
			<view class="address_item address_default">
				<view class="address_item_label">
					设为默认收货地址
				</view>
				<label class="radio">
					<radio checked="true" color="#2588FF" />
				</label>
			</view>
			<view class="address_default" @tap="deleteAddress()">
				<button>删除</button>
			</view>

			<view class="address_add" @tap="sureAddress()">
				<view class="" style="padding: 0 24rpx;">
					<button>确定</button>
				</view>
			</view>
			<!-- <view class="navigation_delete" @tap="deleteAddress()">
				删除
			</view> -->
			
		</view>

		<view class="mask" v-if="deleteFlag == true"></view>
		<view class="delete_mask" v-if="deleteFlag == true">
			<view class="delete_title">
				确定要删除地址吗？
			</view>
			<view class="delete_btns">
				<view class="delete_cancel" @tap="cancelDelete()">
					取消
				</view>
				<view class="delete_sure" @tap="sureDelete()">
					确定
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { editAddress, deleteAddress } from '../../../api/api.js'
	export default {
		data() {
			return {
				deleteFlag: false,
				detail:{
					userName:'',
					userPhone:'',
					userAddress:'',
					isDefault:'1'
				},
				add: {
					sex: '男寝',
					build:'',
					detail:''
				}
			};
		},
		onLoad(e) {
			this.detail = JSON.parse(decodeURIComponent(e.data))
			let arr = this.detail.userAddress.split(' ')
			this.add.sex = arr[0] ? arr[0]: ''
			this.add.build = arr[1] ? arr[1]:''
			this.add.detail = arr[2] ? arr[2]: ''
		},
		methods: {
			setOptions(e) {
				this.add.sex = e.detail.value
			},
			backref() {
				uni.navigateBack({
					delta: 1
				})
			},
			deleteAddress() {
				this.deleteFlag = true;
			},
			sureAddress() {
				if (!this.detail.userName) {
					uni.showToast({
					    icon: 'none',   
					    duration: 3000,
					    title: '请填写姓名'
					});
					return
				}if (!this.detail.userPhone) {
					uni.showToast({
					    icon: 'none',   
					    duration: 3000,
					    title: '请填写手机号码'
					});
					return
				}
				if (!this.add.build) {
					uni.showToast({
					    icon: 'none',   
					    duration: 3000,
					    title: '请填写楼栋号'
					});
					return
				}
				if (!this.add.detail) {
					uni.showToast({
					    icon: 'none',   
					    duration: 3000,
					    title: '请填写寝室号'
					});
					return
				}
				this.detail.userAddress = this.add.sex + ' ' + this.add.build + ' ' + this.add.detail
				uni.showLoading()
				editAddress(this.detail).then(res => {
					if (res.code == 200) {
						uni.showToast({
						    icon: 'none',   
						    duration: 3000,
						    title: res.msg
						});
						uni.navigateBack({
							delta: 1
						})
						uni.hideLoading()
					}
				})
				// uni.navigateTo({
				// 	url: '/pages/memberCenter/address/address'
				// })
			},

			cancelDelete() {
				this.deleteFlag = false;
			},

			sureDelete() {
				deleteAddress({
					id: this.detail.id
				}).then(res => {
					this.deleteFlag = false;
					uni.showToast({
						title: "删除成功",
						icon: 'none',
						duration:2000
					})
					uni.navigateBack({
						delta: 1
					})
				})
				
				
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F4F4F4;
	}

	.navigation {
		width: 100%;
		height: 260rpx;
		background: linear-gradient(180deg, rgba(255, 141, 26, 1) 12.5%, rgba(255, 195, 0, 1) 100%);
		padding: 60rpx 24rpx 0;
		text-align: center;
		position: relative;

		.navigation_back {
			position: absolute;
			top: 60rpx;
		}

		.navigation_title {
			color: #fff;
			font-size: 36rpx;
		}

		.navigation_delete {
			position: absolute;
			right: 32rpx;
			top: 65rpx;
			color: #fff;
			font-size: 32rpx;
		}
	}

	.address_con {
		position: relative;
		z-index: 2;
		padding: 40rpx 32rpx 180rpx;
		border-radius: 32rpx 32rpx 0 0;
		background-color: #F4F4F4;
		margin-top: -100rpx;

		.address_item {
			position: relative;
			display: flex;
			align-items: center;
			height: 100rpx;
			background-color: #fff;
			border-radius: 12rpx;
			margin-bottom: 24rpx;
			padding: 0 24rpx;

			.address_icon {
				// width: 40rpx;
				display: flex;
				justify-content: center;

				image {
					width: 40rpx;
					height: 42rpx;
				}
			}

			.address_input {
				margin-left: 30rpx;

				input {
					color: #333;
					font-size: 26rpx;
					width: 400rpx;

					&.placeholder {
						color: #666;
					}
				}
			}

			.address_more {
				position: absolute;
				right: 24rpx;
			}

			.address_item_label {
				font-size: 30rpx;
				color: #2E343A;
			}

			.radio {
				position: absolute;
				right: 24rpx;
			}
		}
	}

	.address_add {
		position: fixed;
		left: 0;
		bottom: 50rpx;
		width: 100%;
		// padding: 0 24rpx;

		button {
			height: 80rpx;
			background: linear-gradient(180deg, rgba(255, 141, 26, 1) 12.5%, rgba(255, 195, 0, 1) 100%);
			color: #fff;
			font-size: 28rpx;
			font-weight: 700;
			border: 0;
			line-height: 80rpx;
			border-radius: 80rpx;
		}
	}


	.mask {
		position: fixed;
		z-index: 5;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
	}

	.delete_mask {
		position: fixed;
		z-index: 5;
		top: 50%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 610rpx;
		height: 300rpx;
		background-color: #fff;
		padding-top: 80rpx;
		border-radius: 12rpx;

		.delete_title {
			text-align: center;
			color: #2E343A;
			font-size: 32rpx;
			font-weight: 500;
		}

		.delete_btns {
			position: absolute;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 96rpx;
			border-top: 1rpx solid #ccc;
			display: flex;

			.delete_cancel {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 50%;
				height: 96rpx;
				color: #2E343A;
				border-right: 1rpx solid #ccc;
			}

			.delete_sure {
				display: flex;
				justify-content: center;
				align-items: center;
				width: 50%;
				height: 96rpx;
				color: #2588FF;
			}
		}
	}
</style>